/*
 * Copyright (C) 2015 Square, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package plugin.eos.awake.flutter.awake1.network.converter;

import com.google.gson.Gson;

import java.io.IOException;
import java.lang.reflect.Type;

import okhttp3.ResponseBody;
import plugin.eos.awake.flutter.awake1.utils.LogUtils;
import retrofit2.Converter;

final class MyGsonResponseBodyConverter<T> implements Converter<ResponseBody, Object> {
    private final Type type;
    private final Gson gson;

    MyGsonResponseBodyConverter(Gson gson, Type type) {
        this.gson = gson;
        this.type = type;
    }

    @Override
    public Object convert(ResponseBody value) throws IOException {
        String json = value.string();
        try {
//            JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
            // 按标准格式解析
            LogUtils.d("ResponseBody ", "ResponseBody  = " + json);
            return gson.fromJson(json, type);
        } catch (Exception e) {
            throw new RuntimeException(json);
        } finally {
            value.close();
        }
    }
//        return null;
//        try {
//            ApiModel apiModel = (ApiModel)adapter.fromJson(value.charStream());
//            Log.d("xxx5", "model111111 = " + apiModel.code);
//            return apiModel;
//        } finally {
//            value.close();
//        }
}
